package com.start.manager.controller;

import com.start.manager.domain.common.OptionsAll;
import com.start.manager.domain.entity.Company;
import com.start.manager.domain.entity.Goods;
import com.start.manager.domain.vo.OptionsVO;
import com.start.manager.service.ICompanyService;
import com.start.manager.service.IGoodsService;
import com.start.manager.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jian.li
 * @date 2019年 12月30日 11:38:45
 */
@RestController
@RequestMapping(value = "/options")
public class OptionsController {
    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private ICompanyService companyService;

    @GetMapping(value="/getOptions")
    @ResponseBody
    public ResponseResult getOptions(@ModelAttribute OptionsVO optionsVO){
        String flag = optionsVO.getFlag();
        List<OptionsAll> list = null;
        switch (flag){
            case "goods":
                list = getGoodsOption(optionsVO);
                break;
            case "company":
                list = getCompanyOption(optionsVO);
                break;
            default:
                break;
        }
        return ResponseResult.success(list);
    }

    private List<OptionsAll> getCompanyOption(OptionsVO optionsVO) {
        List<Company> list = companyService.getOptionsList(optionsVO);
        List<OptionsAll> listResult = new ArrayList<>();
        list.stream().forEach(company -> {
            OptionsAll optionsAll = new OptionsAll();
            optionsAll.setValue(company.getId());
            optionsAll.setLabel(company.getName());
            listResult.add(optionsAll);
        });
        return listResult;
    }

    private List<OptionsAll> getGoodsOption(OptionsVO optionsVO) {
        List<Goods> list =  goodsService.getOptionsList(optionsVO);
        List<OptionsAll> listResult = new ArrayList<>();
        list.stream().forEach( goods -> {
            OptionsAll optionsAll = new OptionsAll();
            optionsAll.setLabel(goods.getName());
            optionsAll.setValue(goods.getId());
            listResult.add(optionsAll);
        });
        return listResult;
    }


}
